package com.lp.volume;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private AudioManager mAudioManager;
    private TextView mMusicVolumeTv;
    private LinearLayout root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        root = findViewById(R.id.main_root);
        mMusicVolumeTv = findViewById(R.id.main_music_volume_tv);
        SeekBar seekbar = findViewById(R.id.main_seekbar);
        seekbar.setOnSeekBarChangeListener(this);
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //获取系统音量
        int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        //获取当前音乐音量
        int current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        seekbar.setMax(max);
        seekbar.setProgress(current);
        setMusicVolume(current);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        ViewGroup.LayoutParams layoutParams = root.getLayoutParams();
        layoutParams.width= (int) (dm.widthPixels-sp2Px(this,20));
        root.setLayoutParams(layoutParams);

//        LinearLayout.LayoutParams seekbarParams = (LinearLayout.LayoutParams) seekbar.getLayoutParams();
//        seekbarParams.leftMargin= (int) sp2Px(this,15);
//        seekbarParams.rightMargin=(int) sp2Px(this,15);
//        seekbar.setLayoutParams(seekbarParams);
    }

    @SuppressLint("SetTextI18n")
    private void setMusicVolume(int volume) {
        mMusicVolumeTv.setText("媒体音量    当前音量(" + volume + ")");
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Log.d(TAG, "progress   " + progress + "   fromUser   " + fromUser);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        setMusicVolume(progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Log.d(TAG, "onStartTrackingTouch");
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        Log.d(TAG, "onStopTrackingTouch");
        finish();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        return super.onTouchEvent(event);
    }


    public static float sp2Px(Context context, float value) {
        TypedValue typedValue = new TypedValue();
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return TypedValue.applyDimension(2, value, metrics);
    }

    public static int dpToPx(float dp, Resources resources) {
        float px = TypedValue.applyDimension(1, dp, resources.getDisplayMetrics());
        return (int) px;
    }

}
